#ifndef __PROTOCOL_H__
#define __PROTOCOL_H__
#include "devinfo.h"

#define PROTOCOL_HEAD1   0xF1
#define PROTOCOL_HEAD2   0x9F
#define MAC_MAX_SIZE     6     //MAC最大字节数
#define SSID_MAX_SIZE    20    //ssid名字最大字节数
#define MSG_CMD_POS      3     //命令位置
#define MSG_FIX_SIZE     6     //报文固定字节

#define BLE_MAX_NUM      300 //蓝牙最大数量
#define BLE_NAME_MAX     20
#define BLE_MAC_MAX      6

typedef struct
{
  rt_uint8_t name[BLE_NAME_MAX];  // 设备名称
  rt_uint8_t mac[BLE_MAC_MAX]; // 设备mac地址
}bleDataDef;

typedef struct
{
    int           num_devices;//当前蓝牙数量
    rt_uint8_t    id;         //这个主机的ID
    bleDataDef    data[BLE_MAX_NUM];
    rt_uint16_t   crc;        //这个数据的crc
}ble_info_t;



typedef enum {
	PC_CMD_REQPORTED_ACK  = 0x01,//上报数据
	PC_CMD_SET_MODE_REQ   = 0X02,//设置模式请求
	PC_CMD_SET_MODE_ACK   = 0x82,//设置模式响应
	PC_CMD_BOUNDDEV_REQ   = 0x03,//绑定设备请求
	PC_CMD_BOUNDDEV_ACK   = 0x83,//绑定设备响应
	PC_CMD_DEVSLEEP_REQ   = 0x04,//设备休眠请求
	PC_CMD_DEVSLEEP_ACK   = 0x84,//设备休眠响应
	PC_CMD_DEVCAP_REQ     = 0x05,//设备RSSI请求;
	PC_CMD_DEVCAP_ACK     = 0x85,//设备RSSI响应;
	PC_CMD_STOPCAP_REQ    = 0x06,//开启陀螺仪采集请求;
	PC_CMD_STOPCAP_ACK    = 0x86,//开启陀螺仪采集响应;
	PC_CMD_GETDEV_REQ     = 0x07,//获得已绑定设备信息
	PC_CMD_GETDEV_ACK     = 0x87,//获得已绑定设备信息应答
	PC_CMD_GETHOSTINFO_REQ= 0X08,//电脑获取主机信息请求
	PC_CMD_GETHOSTINFO_ACK= 0X88,//电脑获取主机信息应答
}pcCmdDef; //PC端命令枚举;


typedef struct {
	unsigned char head[2];
	unsigned char len;
	unsigned char cmd;
	unsigned char mode;//模式;0:添加扫描模式;1:正常扫描模式
	unsigned char mac[MAC_MAX_SIZE];
	unsigned char ssid[SSID_MAX_SIZE];
	unsigned char x[2];
	unsigned char y[2];
	unsigned char z[2];
	signed   char rssi;
  unsigned char rssiMac[MAC_MAX_SIZE];
  unsigned char rssiSsid[SSID_MAX_SIZE];
	unsigned char KEY[4];//保留字段
	unsigned char crc16[2];
}mcuReporotedDataDef; //mcu上报数据结构体;

typedef struct {
	unsigned char head[2];
	unsigned char len;
	unsigned char cmd;
  unsigned char id[2];//主机ID
	unsigned char mode;//模式;0:添加设备扫描模式;1:正常工作模式
	unsigned char crc16[2];
}mcuSetModeReqDef; //设置模式请求数据结构体;

typedef struct {
	unsigned char head[2];
	unsigned char len;
	unsigned char cmd;
	unsigned char result;
	unsigned char crc16[2];
}mcuSetModeAckDef; //设置模式响应数据结构体;


typedef struct {
	unsigned char head[2];
	unsigned char len;
	unsigned char cmd;
	unsigned char ssid[SSID_MAX_SIZE];
	unsigned char mac[MAC_MAX_SIZE];
	unsigned char id[2];//主机ID
	unsigned char bound;
	unsigned char crc16[2];
}boundDevReqDef; //绑定设备请求数据结构体;

typedef struct {
	unsigned char head[2];
  unsigned char len;
	unsigned char cmd;
	unsigned char ssid[SSID_MAX_SIZE];
	unsigned char mac[MAC_MAX_SIZE];
	unsigned char result;
	unsigned char crc16[2];
}boundDevAckDef; //绑定设备响应数据结构体;


typedef struct {
	unsigned char head[2];
  unsigned char len;
	unsigned char cmd;
	unsigned char ssid[SSID_MAX_SIZE];
	unsigned char mac[MAC_MAX_SIZE];
	unsigned char sleep;//0休眠
	unsigned char crc16[2];
}devSleepReqDef; //设备休眠请求数据结构体;

typedef struct {
	unsigned char head[2];
  unsigned char len;
	unsigned char cmd;
	unsigned char ssid[SSID_MAX_SIZE];
	unsigned char mac[MAC_MAX_SIZE];
	unsigned char result;//0 成功 1失败
	unsigned char crc16[2];
}devSleepAckDef; //设备休眠响应数据结构体;

typedef struct {
	unsigned char head[2];
  unsigned char len;
	unsigned char cmd;
	unsigned char ssid[SSID_MAX_SIZE];
	unsigned char mac[MAC_MAX_SIZE];
  unsigned char type;//测量类型
	unsigned char crc16[2];
}devCapReqDef; //PC请求测量从机数据

typedef struct {
	unsigned char head[2];//报文标记头;
	unsigned char len;
	unsigned char cmd;//命令码;
	unsigned char ssid[SSID_MAX_SIZE];//蓝牙从机SSID名称
	unsigned char mac[MAC_MAX_SIZE];//蓝牙从机MAC地址
	signed   char x[2];//陀螺仪X轴数据
	signed   char y[2];//陀螺仪Y轴数据
	signed   char z[2];//陀螺仪Z轴数据
	unsigned char rssi;//0 成功 1失败
	unsigned char rssiMac[MAC_MAX_SIZE];
  unsigned char rssiSsid[SSID_MAX_SIZE];
	unsigned char key[4]; //按键值
	unsigned char result;//结果
	unsigned char crc16[2];//CRC16校验码;
}devCapAckDef; //pc测量从机数据应答


typedef struct {
	unsigned char head[2];
  unsigned char len;
	unsigned char cmd;
	unsigned char ssid[SSID_MAX_SIZE];
	unsigned char mac[MAC_MAX_SIZE];
	unsigned char crc16[2];
}devStopCapReqDef; //设备陀螺仪关闭请求数据结构体;


typedef struct {
	unsigned char head[2];
  unsigned char len;
	unsigned char cmd;
	unsigned char ssid[SSID_MAX_SIZE];
	unsigned char mac[MAC_MAX_SIZE];
	unsigned char result;
	unsigned char crc16[2];
}devStopCapAckDef; //设备陀螺仪关闭响应数据结构体;


typedef struct {
	unsigned char head[2];
  unsigned char len;
	unsigned char cmd;
	unsigned char pos[2];
	unsigned char crc16[2];
}getBoundDevReqDef;


typedef struct {
	unsigned char head[2];
  unsigned char len;
	unsigned char cmd;
	unsigned char ssid[SSID_MAX_SIZE];
  unsigned char mac[MAC_MAX_SIZE];
  unsigned char result;//0 成功 1失败 2结束
  unsigned char num[2];
	unsigned char crc16[2];
}getBoundDevAckDef;


typedef struct {
	unsigned char head[2];
  unsigned char len;
	unsigned char cmd;
	unsigned char id[2];//0 表示查询其他表示修改主机ID
	unsigned char crc16[2];
}getHostInfoReqDef;

typedef struct {
	unsigned char head[2];
  unsigned char len;
	unsigned char cmd;
	unsigned char id[2];
  unsigned char bleNum[2];
  unsigned char result;//0 成功 1失败 2结束
	unsigned char crc16[2];
}getHostInfoAckDef;


typedef union {
	//扫描主机
	mcuReporotedDataDef mcuReport;
	mcuSetModeReqDef mcuSetModeReq;
	mcuSetModeAckDef mcuSetModeAck;

	//连接主机
	boundDevReqDef    boundDevReq;
	boundDevAckDef    boudnDevAck;
	devSleepReqDef    devSleepReq;
	devSleepAckDef    devSleepAck;
	devCapReqDef      devCapReq;
	devCapAckDef      devCapAck;
	devStopCapReqDef  devStopCapReq;
	devStopCapAckDef  devStopCapAck;
  getBoundDevReqDef getBoundDevReq;
  getBoundDevAckDef getBoundDevAck;
  getHostInfoReqDef getHostInfoReq;
  getHostInfoAckDef getHostInfoAck;
}pcProtoUnionDef; //协议数据结构体;

typedef union {
	pcProtoUnionDef proto;
	unsigned char buf[sizeof(pcProtoUnionDef)];
}pcMsgDef; //接收数据结构体;

typedef struct
{
  pcMsgDef     msg;
  rt_uint8_t   msgLen;
  rt_uint32_t  outtime;//处理超时
  rt_uint8_t   cmd;
}pcMailDef; //pc邮箱定义


void pc_proto_init(void);
rt_err_t send_pc_mail(pcMailDef *mail);
void flash_load_bleInfo(void);

#endif


